package com.example.demo.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import com.example.demo.bean.BreedChicken;
import com.example.demo.bean.Inventory;
import com.example.demo.util.GetData;
import com.example.demo.util.LoginAndToken;
import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
import org.apache.hc.core5.http.ParseException;
import org.apache.hc.core5.net.URIBuilder;
import org.json.JSONArray;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.*;

//获取到存栏数 张栩垄
@Service
public class InventoryNumber {


    public List chickenInventory(String date,String name) throws URISyntaxException, IOException, ParseException {
        HashMap<Object, Object> inventoryNumberMap = new HashMap<>();
        LoginAndToken loginAndToken = new LoginAndToken();
        Map luoHeOrgIdMap = loginAndToken.getOrgIdByName(name);
        Iterator<Map.Entry<Object, Object>> iterator = luoHeOrgIdMap.entrySet().iterator();
        String url = "";
        ArrayList inventoryList = new ArrayList<>();
        url = "http://ny.shuanghui.net:9181/breed/bi/product/houseStock/list";

        URIBuilder uriBuilder;

        //设置参数
        while (iterator.hasNext()){
            uriBuilder = new URIBuilder(url);
            Map.Entry<Object, Object> next1 = iterator.next();
            uriBuilder.setParameter("endDate", date);
            uriBuilder.setParameter("orgId", next1.getValue().toString());
            uriBuilder.setParameter("isComplete", "");
            uriBuilder.setParameter("pageSize", "20");
            uriBuilder.setParameter("pageNum", "1");
            HttpGet httpGet = new HttpGet(uriBuilder.build());
            httpGet.setHeader("Token", LoginAndToken.token);
            CloseableHttpResponse response = LoginAndToken.httpClient.execute(httpGet);
            JSONObject data = GetData.getData(response);
            Inventory inventory = new Inventory();
            Object total = data.getString("total");
            String data1 = data.getString("data");
            JSONObject jsonObject = JSON.parseObject(data1);
            String list = jsonObject.getString("list");
            JSONArray jsonArray = new JSONArray(list);
            //将农场各个栋舍的存栏放到集合中
            HashMap<String, Integer> invQtyMap = new HashMap<>();
            for (int i = 0; i < jsonArray.length(); i++) {
                org.json.JSONObject list1 = jsonArray.getJSONObject(i);
                //栋舍名称
                String houseName = list1.getString("houseName");
                //栋舍对应的存栏
                Integer invQty = (Integer) list1.get("invQty");
                invQtyMap.put(houseName,invQty);
//                inventory.setInvQty(invQty);
            }
            inventory.setInvQty(invQtyMap);
//            System.out.println("存栏" + total);
                inventoryNumberMap.put(next1.getKey(),total);
                inventory.setOrgName(next1.getKey().toString());
                inventory.setTotal(total.toString());
//        }
            inventoryList.add(inventory);
        }
        return inventoryList;
    }

    public List BreedChickenNumber(String date,String name) throws URISyntaxException, IOException, ParseException {
        HashMap<Object, Object> inventoryNumberMap = new HashMap<>();
        LoginAndToken loginAndToken = new LoginAndToken();
        Map luoHeOrgIdMap = loginAndToken.getOrgIdByName(name);
        Iterator<Map.Entry<Object, Object>> iterator = luoHeOrgIdMap.entrySet().iterator();
        String url = "";
        ArrayList inventoryList = new ArrayList<>();
        url = "http://ny.shuanghui.net:9181/breed/bi/breedChicken/houseStock/list";
        URIBuilder uriBuilder;
        //设置参数
        while (iterator.hasNext()) {
            uriBuilder = new URIBuilder(url);
            Map.Entry<Object, Object> next1 = iterator.next();
            uriBuilder.setParameter("endDate", date);
            uriBuilder.setParameter("orgId", next1.getValue().toString());
            uriBuilder.setParameter("isComplete", "N");
            uriBuilder.setParameter("pageSize", "20");
            uriBuilder.setParameter("pageNum", "1");
            HttpGet httpGet = new HttpGet(uriBuilder.build());
            httpGet.setHeader("Token", LoginAndToken.token);
            CloseableHttpResponse response = LoginAndToken.httpClient.execute(httpGet);
            JSONObject data = GetData.getData(response);
            if (name.contains("种鸡")) {
                Integer gNum = (Integer) data.get("gNum");
                Integer mNum = (Integer) data.get("mNum");
                Integer totamNum = (Integer) data.get("totamNum");
                BreedChicken breedChicken = new BreedChicken();
                breedChicken.setgNum(gNum);
                breedChicken.setmNum(mNum);
                breedChicken.setTotamNum(totamNum);
                breedChicken.setOrgName(next1.getKey().toString());
                String list = data.getString("list");
                JSONObject listJson = JSON.parseObject(list);
                String list2 = listJson.getString("list");
                JSONArray jsonArray = new JSONArray(list2);
                System.out.println("1");
                HashMap<String, BreedChicken> breedChickenMap = new HashMap<>();
                for (int i = 0; i < jsonArray.length(); i++) {
                    BreedChicken breedChicken1 = new BreedChicken();
                    org.json.JSONObject list1 = jsonArray.getJSONObject(i);
                    String houseName = list1.getString("houseName");
                    breedChicken1.setOrgName(houseName);
                    Integer cockNum = (Integer) list1.get("cockNum");
                    breedChicken1.setgNum(cockNum);
                    Integer henNum = (Integer) list1.get("henNum");
                    breedChicken1.setmNum(henNum);
                    breedChickenMap.put(houseName, breedChicken1);
//                inventory.setInvQty(invQty);
                }
                breedChicken.setInvQty(breedChickenMap);
                inventoryList.add(breedChicken);

            }
        }
        return inventoryList;
    }
}
